package demo.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Admin
 */
public class IOServer {
  public static void main(String[] args) throws IOException {
    final ServerSocket serverSocket = new ServerSocket(8000);
    new Thread(new Runnable() {
      public void run() {
        while (true) {
          Socket socket = null;
          try {
            socket = serverSocket.accept();
          } catch (IOException e) {
            e.printStackTrace();
          }

          final Socket finalSocket = socket;
          new Thread(new Runnable() {
            public void run() {
              try {
                InputStream inputStream = finalSocket.getInputStream();
                byte[] bytes = new byte[1024];
                int len;
                while ((len = inputStream.read(bytes)) != -1) {
                  System.out.println(new String(bytes, 0, len));
                }
              } catch (IOException e) {
                e.printStackTrace();
              }
            }
          }).start();
        }

      }
    }).start();
  }
}
